
local japi = require 'jass.japi'


-- [[ 复选框 ]]
local checkbox = {}
setmetatable(checkbox,checkbox)

---存放所有的按钮实例
checkbox.allcheckboxs = {}


local mt = extends(ac.frame){}

--- 类型
mt.type = 'checkbox'

--- 魔兽控件类型
mt._type = 'CHECKBOX'

--- 识别名称
mt._name = '复选框'

--默认图标
mt.normal_image = nil 

--按钮禁用下的图标  （没有默认是普通图标）
mt.disabled = nil

--按钮按下状态图标 （没有默认是普通图标）
mt.pushedd = nil 

--对钩图标  启用状态时的勾图标
mt.tick_pushedd = nil

--对钩图标  禁用状态时的勾图标  （没有默认是 对钩图标)
mt.tick_disabled = nil

--- 默认状态 false无勾 | true有勾（从fdf改）
mt.checked = true

--自定义指令
mt.command = nil 

--自定义参数
mt.params = nil 

--- 创建控件
function mt.new(parent, x, y, w, h)
    return class.checkbox:builder
    {
        parent = parent,
        w = w,
        h = h,
        x = x,
        y = y,
    }
end 


--- 输出
function mt:__tostring()
    return string.format('[复选框]->%d',self.handle or 0)
end

---初始化
function mt:init()
    if self.handle then return end 

    local handle = japi.CreateFrameByTagName(self._type, self._name, self.parent.handle, (self.checked and 'Checked' or 'Check'), self._index)
    if not handle or handle == 0 then 
        log.error('创建button失败')
        ac.frame.remove(self)
        return  
    end

    self.handle = handle 

    local childs = japi.FrameGetChildlist(handle)
    
    self.child1 = class.panel.convert(childs[1], self)  --- 正常贴图           BACKDROP
    self.child2 = class.panel.convert(childs[2], self)  --- 按下贴图           BACKDROP
    self.child3 = class.panel.convert(childs[3], self)  --- 禁用贴图           BACKDROP

    if self.pushedd then self:set_pushedd(self.pushedd) end
    if self.disabled then self:set_disabled(self.disabled) end 

    self.child4 = class.panel.convert(childs[4] ,self)  --- 对钩图标 按下贴图           HIGHLIGHT
    self.child5 = class.panel.convert(childs[5] ,self)  --- 对钩图标 禁用贴图           HIGHLIGHT

    if self.tick_pushedd then self:set_tick_pushedd(self.tick_pushedd) end
    if self.tick_disabled then self:set_tick_disabled(self.tick_disabled) end 

    self:set_normal_image(self.normal_image)
    self:set_alpha(self.alpha)
    self:set_width(self.w)
    self:set_height(self.h)
    self:set_position(self.x,self.y)
    self:set_ignore_trackevents(self.ignore_flag)
    self:set_enable(self.enable_flag)
    if self.show_flag then
        self.show_flag = false
        self:show()
    else 
        self.show_flag = true
        self:hide()
    end
    --- 注册监听器
    japi.RegisterFrameEvent(self.handle)

    checkbox.allcheckboxs[handle] = self
    ac.frame.allframes[handle] = self 
    return self
end 

---删除控件
function mt:remove()
    if self.removed then return end
    if self:is_simple() then return end 
    checkbox.allcheckboxs[self.handle] = nil
    ac.frame.remove(self)
end

--- 设置贴图正常
function mt:set_normal_image(image)
    if not image then return end 
    self.child1:set_normal_image(image)
    if self.disabled == nil and self.child3 then 
        self.child3:set_normal_image(image)
    end
    if self.pushedd == nil and self.child2 then  
        self.child2:set_normal_image(image)
    end
end 

--- 设置禁用贴图
function mt:set_disabled(image)
    self.disabled = image
    if self.child3 then 
        self.child3:set_normal_image(image)
    end
end

--- 设置按下贴图
function mt:set_pushedd(image)
    self.pushedd = image
    if self.child2 then
        self.child2:set_normal_image(image)
    end
end

--- 设置按下 对钩图标
function mt:set_tick_pushedd(image)
    self.tick_pushedd = image
    if self.child4 then
        self.child4:set_normal_image(image)
    end
end

--- 设置禁用 对钩图标
function mt:set_tick_disabled(image)
    self.tick_disabled = image
    if self.child5 then 
        self.child5:set_normal_image(image)
    end
end

--- 获取图像
--- @return string texture
function mt:get_normal_image()
    return self.normal_image
end

--- 事件注册
checkbox.events = {

    [7] = function(self) 
        if self:is_simple() then 
            return 
        end 
        self:notify('on_checkbox_ticked')
        if self.command and self.command ~= 'checkbox' then 
            self:notify(('on_%s_ticked'):format(self.command),self.params)
        end 
    end,

    [8] = function(self) 
        self:notify('on_checkbox_cancel')
        if self.command and self.command ~= 'checkbox' then 
            self:notify(('on_%s_cancel'):format(self.command),self.params)
        end 
    end,
}
class.checkbox = mt

return mt